﻿using System;
using System.Windows.Forms;
using MyWcfLib;
using MyWcfMon;

namespace Weather_to_ODBC
{
    public partial class Form1 : Form
    {
            

        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
            locTEXT.Enabled = false;
            apiTEXT.Enabled = false;
            addressTEXT.Enabled = false;
            siButton.Enabled = false;
            impButton.Enabled = false;
            
        }

        void Form1_Load(object sender, EventArgs e)
        {
            Load -= Form1_Load;
            try
            {
                var response = new MyWcfServiceResponse();
                var returnText = string.Empty;
                var sp = new MyWcfServiceProxy("localhost", 8732, "Test", "Test", "Test");
                var command = new MyWcfServiceCommand
                {
                    Command = "Gather stored parameters: ",
                };

                response = sp.ExecuteRead(command);
                returnText = response.Response;
                locTEXT.Enabled = true;
                apiTEXT.Enabled = true;
                addressTEXT.Enabled = true;
                siButton.Enabled = true;
                impButton.Enabled = true;

                if (returnText == "Success")
                {
                    locTEXT.Text = response.areaID;
                    apiTEXT.Text = response.API;
                    addressTEXT.Text = response.deviceInst;
                    if (response.swspd.ToLower().Contains("kmph"))
                    {
                        siButton.Checked = true;
                        impButton.Checked = false;
                    }
                    else
                    {
                        siButton.Checked = false;
                        impButton.Checked = true;
                    }
                }
                else
                {
                    var msg = "Results were not found by the service: " + returnText;
                    MessageBox.Show(msg);
                }
            }
            catch (Exception ex)
            {
                var msg = "Error occurred loading the stored parameters: " + ex.Message;
                MessageBox.Show(msg);

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sLOC;
            string sAPI;
            string sDEVADD;
            string sltemp, shtemp, swspd, sctemp;
            
			//set up variables based on user input
            sLOC = locTEXT.Text;
            sAPI = apiTEXT.Text;
            //sURL = "http://free.worldweatheronline.com/feed/weather.ashx?q=" + sLOC + "&format=xml&num_of_days=5&key="+sAPI;
            sDEVADD = addressTEXT.Text;
            //sDSN = "Delta Network";

            //initialize the index and xml tags
            
            sltemp = "tempMinC";
            shtemp = "tempMaxC";
            sctemp = "temp_C";
            swspd = "windspeedKmph";


            try
            {
                //set up imperial or metric XML tags
                if (siButton.Checked)
                {
                    impButton.Checked = false;
                    sltemp = "tempMinC";
                    shtemp = "tempMaxC";
                    sctemp = "temp_C";
                    swspd = "windspeedKmph";
                }

                if (impButton.Checked)
                {
                    siButton.Checked = false;
                    sltemp = "tempMinF";
                    shtemp = "tempMaxF";
                    sctemp = "temp_F";
                    swspd = "windspeedMiles";
                }

                var response = new MyWcfServiceResponse();
                var returnText = string.Empty;
                var sp = new MyWcfServiceProxy("localhost", 8732, "Test", "Test", "Test");
                var command = new MyWcfServiceCommand
                {
                    Command = "Apply new settings: ",

                    scTemp = sctemp,
                    slTemp = sltemp,
                    shTemp = shtemp,
                    swspd = swspd,
                    deviceInst = sDEVADD,
                    areaID = sLOC,
                    API = sAPI


                };

                response = sp.ExecuteCommand(command);
                returnText = response.Response;
                MessageBox.Show("Operation completed: " + returnText,"Results");



            }
            catch (Exception ex)
            {
                var msg =
                    "There was either an error generating the RSS feed or connecting to the ODBC driver. Please ensure that you have entered a valid www.worldwideweatheronline.com API key and have correctly configured the Delta ODBC driver: " +
                    ex.Message;
                MessageBox.Show(msg, "Execution Error");
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult result;
            string sline1 = "Navigate to http://www.worldweatheronline.com/register.aspx and registor for a FREE RSS API Key.  ";
            string sline2 = "Enter the desired Zip/Postal Code, the BACnet controller address and the API Key. Select whether you want metric or imperial results. ";
            string sline3 = "Would you like to vist the world weather online API registration site? ";
            result = MessageBox.Show(sline1 + sline2 + sline3, "Instructions", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                System.Diagnostics.Process.Start("http://www.worldweatheronline.com/register.aspx");
            }
           
            
        }

        private void clsButton_Click(object sender, EventArgs e)
        {
            Close();
        }

    }
}
